using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol.POD;
using System.Threading;
using UnityEngine;
using UnityEngine.Timeline;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 临时的进入游戏类型
    /// </summary>
    public enum EnterGameType
    {
        Level = 2,
        Battle = 1,
        Login = 0,
    }

    public class ProcedureGame : ProcedureBase
    {
        static ProcedureGame()
        {
            // Timeline信号点全局监听 
            ExSignalTrack.SetReceiverCreator((_) => new ExSignalReceiver());
        }

        public static EnterGameType enterMode = EnterGameType.Login;

        public override void OnEnter()
        {
            Debug.Log("Enter game");
            BehaviacGlobal.Instance.Init((string path) =>
            {
                if (Thread.CurrentThread.ManagedThreadId == ManagerCenter.mainThreadId)
                {
                    return LoadBehaviac(path);
                }
                else
                {
                    return LoadBehaviacOnMainThread(path).AsTask().Result;
                }
            });
            CFStateMachineManager.Instance.Init(new CFSMViewResLoaderCreater(), new CFSMBlackBoard(), () => FrameworkEntry.Instance.FrameRate);
            SkillFactory.SetLoadFunc((path) => { return SkillViewUtil.Load(path); });

            //EnterScene();
            //return;

            switch (enterMode)
            {
                case EnterGameType.Level:
                    TestLevelPlay().Forget();
                    break;
                case EnterGameType.Battle:
                    TestBattle();
                    break;
                case EnterGameType.Login:
                    EnterGameMain();
                    break;
            }

        }

        private async UniTask<byte[]> LoadBehaviacOnMainThread(string path)
        {
            await UniTask.SwitchToMainThread();
            return LoadBehaviac(path);
        }

        private byte[] LoadBehaviac(string path)
        {
            ResLoader resloader = ResLoader.Create();
            TextAsset asset = resloader.LoadABAsset<TextAsset>(path);
            if (asset == null)
            {
                return null;
            }
            byte[] bytes = asset.bytes;
            resloader.Put2Pool();
            return bytes;
        }

        void EnterGameMain()
        {
            GameplayAreaManager.Instance.Switch(GameplayAreaType.MainCity);
        }

        /// <summary>
        /// 测试进入玩法demo
        /// </summary>
        async UniTask TestLevelPlay()
        {
            CreateLevelPOD levelPOD = FakeDataCenter.Instance.levelPOD;
            //初始化关卡
            await GameplayAreaManager.Instance.SwitchAwait(GameplayAreaType.Level, (levelPOD.cid, true));
            CLevelLogicModule.Instance.InitLocalLevel(levelPOD, FakeDataCenter.Instance.levelSavePOD, FakeDataCenter.Instance.CreateLogicPlayerPOD());
        }

        void TestBattle()
        {
            GameObject.Find("LaunchCamera").SetActive(false);
            var fake = FakeDataCenter.Instance;
            var battlePOD = fake.CreateBattlePOD();
            // await BattleManager.Instance.PrepareBattle(battlePOD,true);
            CBattleLogicModule.Instance.InitLocalBattle(battlePOD, FakeDataCenter.Instance.CreateLogicPlayerPOD());
        }

        public override void OnLeave()
        {
            BehaviacGlobal.Instance.Shutdown();
            GameDataCenter.Clear();
            GameplayAreaManager.Instance.QuitGame();
        }

        public override void OnUpdate(float deltaTime, float unscaledDeltaTime)
        {

        }
    }
}
